import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lamp_app/page/home/widget/echart_page.dart';
import '../../data/remote/home_info.dart';
import '../../domain/repository/home_repository.dart';
import '../../generated/assets.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final dio = Dio();
  List<Widget> ListItems = [];

  int? num1 = 0;
  int? num2 = 0;
  int? num3 = 0;
  List<String?> timeArr = [];
  List<int?> faultArr = [];
  List<int?> onArr = [];

  @override
  void initState() {
    super.initState();
    getFetch();
    getList();
  }

  Future<void> getFetch() async {
    try {
      final response = await dio.get('http://www.cqjhzn.com/api/stat/data');
      if (response.statusCode == 200) {
        final data = response.data;
        print('data444===>${data}');

        HomeCommonData? newData = HomeCommonData?.fromJson(response.data);
        print(
            'newData-lampTrendChart===>${newData.data?.lampTrendChart?.onArr}');
        setState(() {
          num1 = newData.data?.lampCount;
          num2 = newData.data?.lampOnPencent;
          num3 = newData.data?.lampFaultPencent;
          onArr = newData.data?.lampTrendChart?.onArr as List<int>;
          faultArr = newData.data?.lampTrendChart?.faultArr as List<int>;
          timeArr = newData.data?.lampTrendChart?.timeArr as List<String>;
        });
      }
    } catch (e) {
      print('Error: $e');
    }
  }

  Future<void> getList() async {
    try {
      final response = await dio.get('http://www.cqjhzn.com/api/stat/lamp');
      if (response.statusCode == 200) {
        final data = response.data;
        List<Widget> list = [];
        print('data555===>${data}');

        HomeLampList? newData = HomeLampList?.fromJson(response.data);

        print('totaltotaltotal===>${newData.data!.length}');
        for (int i = 0; i < newData.data!.length; i++) {
          HomeLampData? item = newData.data?[i];
          list.add(
            Container(
              padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.fromLTRB(0, 0, 0, 10),

                    // shape: RoundedRectangleBorder(
                    //     borderRadius: BorderRadius.circular(10.0)),
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Color(0xFF192C44), // 边框
                      boxShadow: [
                        BoxShadow(
                          color: Color(0xFF059BFF),
                          blurRadius: 5,
                        ),
                      ],
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(20),
                      child: Column(
                        // mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            // mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                '灯具',
                                style: TextStyle(
                                    color: Color(0xFFA5B2BE),
                                    fontSize: 18,
                                    fontWeight: FontWeight.w600),
                              ),
                              SizedBox(width: 10),
                              SizedBox(
                                width: 67.5,
                                height: 28.5,
                                child: Stack(
                                  children: [
                                    Image.asset(
                                      Assets.imagesNumBg,
                                      // width: 45,
                                      // height: 19,
                                      // fit: BoxFit.cover, // 根据需要选择合适的fit模式
                                    ),
                                    Align(
                                      child: Text(
                                        (i + 1).toString(),
                                        style: TextStyle(
                                            color: Color(0xFFC1C3D3),
                                            fontSize: 18,
                                            fontWeight: FontWeight.w600),
                                      ),
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),

                          Text.rich(
                            TextSpan(
                              text: '编号：',
                              style: TextStyle(color: Color(0xFFA5B2BE)),
                              children: <InlineSpan>[
                                TextSpan(
                                  text: item?.imei,
                                  style: TextStyle(color: Color(0xFFC1C3D3)),
                                ),
                              ],
                            ),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Text.rich(
                            TextSpan(
                              text: '坐标：',
                              style: TextStyle(color: Color(0xFFA5B2BE)),
                              children: <InlineSpan>[
                                TextSpan(
                                  text: item?.data?.coordinates ?? '暂无',
                                  style: TextStyle(color: Color(0xFFC1C3D3)),
                                ),
                              ],
                            ),
                          ),
                          // Text.rich(
                          //   TextSpan(
                          //     text: '状态：',
                          //     style: TextStyle(color: Color(0xFFA5B2BE)),
                          //     children: <InlineSpan>[
                          //       TextSpan(
                          //         text: item?.statusName,
                          //         style: TextStyle(color: Color(0xFFC1C3D3)),
                          //       ),
                          //     ],
                          //   ),
                          // ),

                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            // mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                '状态：',
                                style: TextStyle(color: Color(0xFFA5B2BE)),
                              ),
                              SizedBox(width: 5),
                              if (item?.statusName == '开启')
                                SizedBox(
                                  width: 67.5,
                                  height: 28.5,
                                  child: Stack(
                                    children: [
                                      Image.asset(
                                        Assets.imagesIcStatus1,
                                        // width: 45,
                                        // height: 19,
                                        // fit: BoxFit.cover, // 根据需要选择合适的fit模式
                                      ),
                                      Positioned(
                                        top: 0,
                                        left: 20,
                                        child: Text(
                                          item!.statusName.toString(),
                                          style: TextStyle(
                                              color: Color(0xFFC1C3D3),
                                              fontWeight: FontWeight.w600),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              if (item?.statusName == '离线')
                                SizedBox(
                                  width: 67.5,
                                  height: 28.5,
                                  child: Stack(
                                    children: [
                                      Image.asset(
                                        Assets.imagesIcStatus2,
                                        // width: 45,
                                        // height: 19,
                                        // fit: BoxFit.cover, // 根据需要选择合适的fit模式
                                      ),
                                      Positioned(
                                        top: 0,
                                        left: 20,
                                        child: Text(
                                          item!.statusName.toString(),
                                          style: TextStyle(
                                              color: Color(0xFFC1C3D3),
                                              fontWeight: FontWeight.w600),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              if (item?.statusName == '故障')
                                SizedBox(
                                  width: 67.5,
                                  height: 28.5,
                                  child: Stack(
                                    children: [
                                      Image.asset(
                                        Assets.imagesIcStatus3,
                                        // width: 45,
                                        // height: 19,
                                        // fit: BoxFit.cover, // 根据需要选择合适的fit模式
                                      ),
                                      Positioned(
                                        top: 0,
                                        left: 20,
                                        child: Text(
                                          item!.statusName.toString(),
                                          style: TextStyle(
                                              color: Color(0xFFC1C3D3),
                                              fontWeight: FontWeight.w600),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              if (item?.statusName == '关闭')
                                SizedBox(
                                  width: 67.5,
                                  height: 28.5,
                                  child: Stack(
                                    children: [
                                      Image.asset(
                                        Assets.imagesIcStatus4,
                                        // width: 45,
                                        // height: 19,
                                        // fit: BoxFit.cover, // 根据需要选择合适的fit模式
                                      ),
                                      Positioned(
                                        top: 0,
                                        left: 20,
                                        child: Text(
                                          item!.statusName.toString(),
                                          style: TextStyle(
                                              color: Color(0xFFC1C3D3),
                                              fontWeight: FontWeight.w600),
                                        ),
                                      )
                                    ],
                                  ),
                                )
                            ],
                          ),

                          SizedBox(
                            height: 10,
                          ),
                          Text.rich(
                            TextSpan(
                              text: '厂家：',
                              style: TextStyle(color: Color(0xFFA5B2BE)),
                              children: <InlineSpan>[
                                TextSpan(
                                  text: item?.factory,
                                  style: TextStyle(color: Color(0xFFC1C3D3)),
                                ),
                              ],
                            ),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Text.rich(
                            TextSpan(
                              text: '最后采集时间：',
                              style: TextStyle(color: Color(0xFFA5B2BE)),
                              children: <InlineSpan>[
                                TextSpan(
                                  text: item?.lastUpTime,
                                  style: TextStyle(color: Color(0xFFC1C3D3)),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          );
        }
        print('list===>${list}');
        setState(() {
          ListItems = list;
        });
      }
    } catch (e) {
      print('Error: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.topStart,
      children: [
        Image.asset(Assets.imagesHomeBg,
            width: double.infinity, height: double.infinity, fit: BoxFit.cover),
        SingleChildScrollView(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 40,
            ),
            Stack(
              // alignment:AlignmentDirectional.centerStart,
              children: [
                Image.asset(Assets.imagesHomeTopBg,
                    width: double.infinity, fit: BoxFit.cover),
                Center(
                  child: Image.asset(
                    Assets.imagesHomeTitle,
                    width: 130,
                    // height: 31,
                  ),
                )
              ],
            ),
            SizedBox(
              height: 20,
            ),
            Stack(
              children: [
                Image.asset(Assets.imagesIcTitleBg,
                    width: double.infinity, fit: BoxFit.cover),
                Padding(
                  padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                  child: Text(
                    '灯具总数',
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  ),
                )
              ],
            ),
            Container(
                width: double.infinity,
                // height: 200,
                margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
                padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
                decoration: BoxDecoration(color: Color(0xFF1D3147)),
                child: Row(
                  children: [
                    Image.asset(
                      Assets.imagesIcHomeIcon1,
                      width: 100,
                      height: 100,
                      // height: 31,
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(num1.toString(),
                            style: TextStyle(
                                color: Color(0xFF46DAFF),
                                fontWeight: FontWeight.w600,
                                fontSize: 36)),
                      ],
                    )
                  ],
                )),
            SizedBox(
              height: 20,
            ),
            Stack(
              children: [
                Image.asset(Assets.imagesIcTitleBg,
                    width: double.infinity, fit: BoxFit.cover),
                Padding(
                  padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                  child: Text(
                    '亮灯率',
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  ),
                )
              ],
            ),
            Container(
                width: double.infinity,
                // height: 200,
                margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
                padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
                decoration: BoxDecoration(color: Color(0xFF1D3147)),
                child: Row(
                  children: [
                    Image.asset(
                      Assets.imagesIcHomeIcon2,
                      width: 100,
                      height: 100,
                      // height: 31,
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(num2.toString() + '%',
                            style: TextStyle(
                                color: Color(0xFF46DAFF),
                                fontWeight: FontWeight.w600,
                                fontSize: 36)),
                      ],
                    )
                  ],
                )),
            SizedBox(
              height: 20,
            ),
            Stack(
              children: [
                Image.asset(Assets.imagesIcTitleBg,
                    width: double.infinity, fit: BoxFit.cover),
                Padding(
                  padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                  child: Text(
                    '故障率',
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  ),
                )
              ],
            ),
            Container(
                width: double.infinity,
                // height: 200,
                margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
                padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
                decoration: BoxDecoration(color: Color(0xFF1D3147)),
                child: Row(
                  children: [
                    Image.asset(
                      Assets.imagesIcHomeIcon3,
                      width: 100,
                      height: 100,
                      // height: 31,
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(num3.toString() + '%',
                            style: TextStyle(
                                color: Color(0xFF46DAFF),
                                fontWeight: FontWeight.w600,
                                fontSize: 36)),
                      ],
                    )
                  ],
                )),
            SizedBox(
              height: 40,
            ),
            // Container(
            //   width: double.infinity,
            //   height: 250,
            //   padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
            //   child: StackedAreaCustomColorLineChart(),
            // ),
            EchartPage(
              timeArr: timeArr,
              onArr: onArr,
              faultArr: faultArr,
            ),
            SizedBox(
              height: 40,
            ),
            ...ListItems,

            SizedBox(
              height: 80,
            ),
          ],
        )),
      ],
    );
  }
}
